import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

import '../../config/config.dart';
import '../../generated/assets.dart';
import '../../language.dart';
import '../../routers/routers.dart';
import '../../utils/singleton_manager.dart';
import '../../views/common_widget.dart';
import 'language_logic.dart';

class LanguagePage extends StatelessWidget {
  LanguagePage({Key? key}) : super(key: key);

  final logic = Get.find<LanguageLogic>();

  getListItem(String title, String tag) {
    return GestureDetector(
        onTap: () async {
          if (SingletonManager().appLanguage == tag) {
            return;
          }
          final box = GetStorage();
          box.write(Config.languageKey, tag);
          logic.currentLanguage.value = tag;
          switch (logic.currentLanguage.value) {
            case 'zh_TW':
              Get.updateLocale(const Locale("zh", "TW"));
              SingletonManager().appLanguage = 'zh_TW';
              break;
            case 'zh_CN':
              Get.updateLocale(const Locale("zh", "CN"));
              SingletonManager().appLanguage = 'zh_CN';
              break;
            case 'en_US':
              Get.updateLocale(const Locale("en", "US"));
              SingletonManager().appLanguage = 'en_US';
              break;
          }
          Future.delayed(const Duration(milliseconds: 300), () {
            // var data = {};
            // if (logic.fromRouter == Routes.setting) {
            //   data['gotoMine'] = true;
            // } else if (logic.fromRouter == Routes.login) {
            //   data['gotoLogin'] = true;
            // }
            // Get.offAllNamed(Routes.home, arguments: data);
            Get.forceAppUpdate();
          });
        },
        child: Container(
          height: 124.w,
          decoration: BoxDecoration(
            color: Config.colorBg2.withOpacity(0.88),
            borderRadius: BorderRadius.circular(Config.roundMini.w),
          ),
          padding: EdgeInsets.symmetric(horizontal: 30.w),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Text(title, style: TextStyle(fontSize: Config.textSizeSubTitle.sp, height: 1, color: Config.colorText1)),
              Expanded(
                child: Visibility(
                  visible: tag == logic.currentLanguage.value,
                  child: Container(
                    alignment: Alignment.centerRight,
                    child: Icon(
                      Icons.check_rounded,
                      size: 45.w,
                      color: Config.colorMain,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg,
      body: Stack(
        children: [
          Column(
            children: [
              CommonWidget.commonAppBar(
                LanguageMsg.langChange.tr,
              ),
              Expanded(
                child: Container(
                  width: double.infinity,
                  padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0.w),
                  margin: EdgeInsets.symmetric(vertical: 26.w, horizontal: 34.w),
                  // decoration: BoxDecoration(
                  //   color: Config.colorBg2,
                  //   borderRadius: BorderRadius.circular(12.w),
                  // ),
                  child: Obx(
                        () => ListView(
                      physics: const NeverScrollableScrollPhysics(),
                      padding: EdgeInsets.symmetric(vertical: 0.w, horizontal: 0.w),
                      children: [
                        getListItem("简体中文", 'zh_CN'),
                        Divider(
                          height: 22.w,
                          color: Colors.transparent,
                        ),
                        // getListItem("繁體中文", 'zh_TW'),
                        // Divider(
                        //   height: 20.w,
                        //   color: Colors.transparent,
                        // ),
                        getListItem("English", 'en_US'),
                      ],
                    ),
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }
}
